home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / bplus20.zip / LISTTREE.C < prev    next >
Text File  |  1987-11-12  |  3KB  |  89 lines

  1. /*************************************************************************/
  2. /*                                                                       */
  3. /*                             LISTTREE.C                                */
  4. /*                                                                       */
  5. /*  This program displays each block in an index file.  The root node    */
  6. /*  of the index tree is displayed first.  Then the left most descendant */
  7. /*  block is displayed for each level in the tree.  After the leaf block */
  8. /*  is displayed, the right most descendant from the ancestor block is   */
  9. /*  displayed.  That is, a preordering method of transversing the tree   */
  10. /*  is used.  The complete block is displayed including the data file    */
  11. /*  address, the index file address, and the index key.  The printtree   */
  12. /*  routine is recursive.                                                */
  13. /*                                                                       */
  14. /*************************************************************************/
  15.  
  16. #include <stdio.h>
  17. #include "bplus.h"
  18.  
  19. /* constant and macros */
  20. #define  NULLREC  (-1L)
  21. #define  ENT_ADR(pb,off)  ((ENTRY*)((char*)((pb)->entries) + off))
  22. #define  ENT_SIZE(pe)     strlen((pe)->key) + 1 + 2 * sizeof(RECPOS)
  23.  
  24. /* global variables defined in BPLUS.C */
  25. extern  IX_DESC    *pci;
  26. extern  BLOCK      *block_ptr;
  27.  
  28. IX_DESC ixfile;
  29.  
  30. void pascal retrieve_block(int, RECPOS);
  31. int pascal copy_entry(ENTRY *, ENTRY *);
  32.  
  33. void print_blk(pb)                 /* list each entry in block pb */
  34.    BLOCK *pb;
  35.   {
  36.     int i;
  37.     i = 0;
  38.     printf("%ld  ",pb->brec);
  39.     printf("%d  %ld  ",pb->bend,pb->p0);
  40.     while (i < pb->bend)
  41.       {
  42.         printf(" %ld ",ENT_ADR(pb,i) -> idxptr);
  43.         printf(" %ld ",ENT_ADR(pb,i) -> recptr);
  44.         printf("%s    ",ENT_ADR(pb,i) -> key);
  45.         i = i + ENT_SIZE(ENT_ADR(pb,i));
  46.       }
  47.     printf("\n");
  48.   }
  49.  
  50. void printtree(num, l, level)             /* print the index tree */
  51.    RECPOS num;                            /* index block address */
  52.    int l;                                 /* indent each level l */
  53.    int level;                             /* level in index file */
  54.   {
  55.     long  address;
  56.     int  end, i, j;
  57.     if (num != NULLREC)
  58.       {
  59.         for (i=1; i<=l; i++)
  60.           printf("    ");
  61.         retrieve_block(level, num);
  62.         print_blk(block_ptr);
  63.         address = block_ptr -> p0;
  64.         printtree(address, l + 1,level + 1);
  65.         retrieve_block(level, num);
  66.         j = 0;
  67.         end = block_ptr -> bend;
  68.         while (j < end)
  69.         {
  70.           address = ENT_ADR(block_ptr,j) -> idxptr;
  71.           printtree(address, l + 1, level + 1);
  72.           retrieve_block(level, num);
  73.           j = j + ENT_SIZE(ENT_ADR(block_ptr,j));
  74.         }
  75.       }
  76.     }
  77.  
  78. main()
  79.   {
  80.     char name[80];
  81.  
  82.     printf("\n\n                       Display An Index Tree\n\n");
  83.     printf("     Name of index file: ");
  84.     gets(name);
  85.     open_index(name,&ixfile, 1);
  86.     printtree(0L,1,0);
  87.     close_index(&ixfile);
  88.   }
  89.